using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[AddComponentMenu("GFramework/Skin Mesh Part")]
[ExecuteInEditMode]
public class SkinnedMeshPart : MonoBehaviour
{
	//public string rootBone;
	public List<string> bonesName;
	public bool autoMapBones = false;

	// Use this for initialization
	void Start () {
		if (autoMapBones)
			MapMeshToParentBones(transform.parent);
	}

#if UNITY_EDITOR
	void LateUpdate()
	{
		if (autoMapBones)
		{
			MapMeshToParentBones(transform.parent);
			autoMapBones = false;
		}
	}
#endif

	public void MapMeshToParentBones(Transform root)
	{
		SkinnedMeshRenderer renderer = GetComponent<SkinnedMeshRenderer>();
		Transform container = root;
		if (renderer != null &&
			bonesName != null && bonesName.Count > 0 &&
			container != null)
		{
			Transform[] bones = new Transform[bonesName.Count];
			for (int i = 0; i < bonesName.Count; i++)
			{
				bones[i] = container.Find(bonesName[i]);
			}
			renderer.bones = bones;
		}
	}
}
